function [cmort] = get_mortclass(ptstruct,DOPLOT)
%GET_MORTCLASS	get patient ids for each class (death - 2, alive - 1)
%	[cmort] = get_mortclass(ptstruct,DOPLOT) 
%	
%
%	Inputs:     ptstruct    patient structure
%		
%		
%
%	Outputs:    cmort       output with pt ids and los for each class
%		
%		
%	
%	See also ana_dynamics

%	References: N/A
%	
%	

%	Copyright 2013 MAF Pimentel
%	This program is free software: you can redistribute it and/or modify
%	it under the terms of the GNU General Public License as published by
%	the Free Software Foundation, either version 3 of the License, or
%	(at your option) any later version.
%	
%	This program is distributed in the hope that it will be useful,
%	but WITHOUT ANY WARRANTY; without even the implied warranty of
%	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%	GNU General Public License for more details.
%	
%	You should have received a copy of the GNU General Public License
%	along with this program.  If not, see <http://www.gnu.org/licenses/>.


%	$Author: MAF Pimentel$
%	$Revision: 1.0.0.0$
%	$Date: 03-Jun-2013 09:56:09$
%	Contact: marco.and.pimentel@gmail.com
%	Originally written on: PCWIN64

if nargin < 2
    DOPLOT = 1;
end

N = length(ptstruct);

for i = 1 : 2
    cmort{i}.los = [];
    cmort{i}.id = [];
end

for i = 1 : N                           % Loop over all patients
    if ptstruct(i).nicus(1) == 1        % Patients with only one ICU admission
        if ~isempty(ptstruct(i).data{1}.hr) && ...
                ~isempty(ptstruct(i).data{1}.pulsep) && ...
                ~isempty(ptstruct(i).data{1}.uo) && ...
                ~isempty(ptstruct(i).data{1}.lactate) % Patients with pp and hr data
            nmort = ptstruct(i).clinics.mort28d; % mortality category
            cmort{nmort+1}.id = [cmort{nmort+1}.id i];
            cmort{nmort+1}.los = [cmort{nmort+1}.los ptstruct(i).los];
        end
    end
end

if DOPLOT
figure;
for i = 1 : 2
    subplot(1,2,i);
    h=cdfplot(cmort{i}.los); xlim([0 75]); grid off; xlabel('LoS (days)'); 
    title(sprintf('LVEF %d, nPts %d',i,length(cmort{i}.los)));
    xdata = get(h,'XData'); ydata = get(h,'YData'); hold on;
    [~, ip] = min(abs(xdata-prctile(cmort{i}.los,95)));
    plot([0 xdata(ip) xdata(ip)], [ydata(ip) ydata(ip) 0], 'r'); 
    [~, ip] = min(abs(xdata-prctile(cmort{i}.los,75)));
    plot([0 xdata(ip) xdata(ip)], [ydata(ip) ydata(ip) 0], 'r');
    [~, ip] = min(abs(xdata-prctile(cmort{i}.los,50)));
    plot([0 xdata(ip) xdata(ip)], [ydata(ip) ydata(ip) 0], 'r'); hold off;
end
end

end